package com.qch.rocket.demo.producer;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageQueue;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.List;

/**
 * @Author qch
 * 部分有序消息
 * @Date 2021/6/9
 */
public class MyOrderPartProducer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("producer_11_grp_01");
        producer.setNamesrvAddr("192.168.142.134:9876");
        producer.start();
        Message message=null;
        MessageQueue messageQueue=null;
        //获取指定主题的MQ列表
        List<MessageQueue> messageQueues = producer.fetchPublishMessageQueues("tp_demo_11");
        for (int i = 0; i < 100; i++) {
            //采取轮询的方式指定MQ,发送订单消息，保证同一个订单消息按顺序发送到同一个mQ
            messageQueue=messageQueues.get(i%8);
             message = new Message("tp_demo_11", ("hello qch order part create"+i).getBytes());
            SendResult result = producer.send(message,messageQueue);
             message = new Message("tp_demo_11", ("hello qch order part pay"+i).getBytes());
            producer.send(message,messageQueue);
             message = new Message("tp_demo_11", ("hello qch order part ship"+i).getBytes());
            producer.send(message,messageQueue);


        }

        producer.shutdown();


    }
}
